package com.atguigu.system.service.impl;

import com.atguigu.model.dto.SysCheckWorkDTO;
import com.atguigu.model.system.SysCalendar;
import com.atguigu.model.system.SysCheckWork;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.SysCheckWorkVo;
import com.atguigu.system.mapper.SysCalendarMapper;
import com.atguigu.system.mapper.SysCheckWorkMapper;
import com.atguigu.system.mapper.SysUserMapper;
import com.atguigu.system.service.SysCheckWorkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 考勤表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-06-19
 */
@Service
public class SysCheckWorkServiceImpl extends ServiceImpl<SysCheckWorkMapper, SysCheckWork> implements SysCheckWorkService {

    @Resource
    private SysCheckWorkMapper mapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysCalendarMapper sysCalendarMapper;

    @Override
    public void autoCheckWork(SysCheckWorkVo vo) {
        Integer month = vo.getMonth();
        if (Objects.isNull(month)){
            month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        }
        List<String> personList = vo.getPersonList();
        if (CollectionUtils.isEmpty(personList)){
            return;
        }

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("name",personList);
        List<SysUser> sysUsers = sysUserMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(sysUsers)){
            return;
        }

        QueryWrapper<SysCalendar> wrapper = new QueryWrapper<>();
        wrapper.eq("sys_month",month);
        List<SysCalendar> sysCalendars = sysCalendarMapper.selectList(wrapper);
        List<SysCheckWork> dataList = new ArrayList<>(sysCalendars.size() * sysUsers.size());
        for (SysUser user : sysUsers) {
            String userId = user.getId();
            for (SysCalendar calendar : sysCalendars) {
                SysCheckWork checkWork = new SysCheckWork();
                checkWork.setUserId(Long.valueOf(userId));
                checkWork.setCalendarId(calendar.getId());
                checkWork.setCheckDate(calendar.getSysDate());
                String isWeekend = calendar.getIsWeekend();
                if (!isWeekend.equals("日")){
                    checkWork.setStartTime("11:00");
                    checkWork.setEndTime("21:00");
                    checkWork.setHowLong("11");
                }
                dataList.add(checkWork);
            }
        }
        mapper.batchInsert(dataList);
    }

    @Override
    public IPage<SysCheckWork> selectPage(Page<SysCheckWork> pageParam, SysCheckWorkVo vo) {
        if (vo == null || StringUtils.isEmpty(vo.getPerson()) || StringUtils.isEmpty(vo.getMonth())){
            return null;
        }
        return baseMapper.selectPage(pageParam,vo);
    }
}
